<template>

    <div>
        <el-container>
            <el-header class="homeHeader">
                <div class="tittle">
                    国电煤炭系统
                </div>
                <el-dropdown @command="commandHandler">
                    <span class="el-dropdown-link" style="color: #fff">
                        {{user.name}} <i class="el-icon-arrow-down el-icon--right"></i>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="updatePassword">修改密码</el-dropdown-item>
                            <el-dropdown-item command="logout">注销</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <el-container>
                <el-aside id="el-aside">
                    <el-menu active-text-color="#409eff" text-color="#606266">
                        <el-menu-item v-for="(menu) in functions" :index="menu.id" :key="menu.id"
                                      @click="getView(menu.id)">
                            <i :class="menu.icon"></i>
                            <span slot="title">{{ menu.name }}</span>
                        </el-menu-item>
                    </el-menu>
                </el-aside>
                <el-main>
                    <div class="homeWelcome" v-if="this.$router.currentRoute.value.path === '/home'">
                        欢迎使用国电煤炭交易平台后台系统!
                    </div>
                    <router-view class="homeRouterView"/>
                </el-main>
            </el-container>
        </el-container>
    </div>
    <el-dialog :close-on-click-modal="false" title="修改密码" v-model="updatePasswordDialog" width="380px">
        <el-form class="dialog-form" :model="updatePasswordForm"
                 label-width="100px"
                 label-position="right"
                 ref="updatePasswordForm"
                 :rules="rules">
            <el-form-item label="旧密码:" prop="oldPassword" style="margin-bottom: 40px">
                <el-input v-model="updatePasswordForm.oldPassword"
                          show-password
                          clearable
                          placeholder="请输入旧密码"
                          style="width: 190px;">
                </el-input>
            </el-form-item>
            <el-form-item label="新密码:" prop="newPassword" style="margin-bottom: 40px">
                <el-input v-model="updatePasswordForm.newPassword"
                          show-password
                          clearable
                          placeholder="请输入新密码"
                          style="width: 190px;">
                </el-input>
            </el-form-item>
            <el-form-item label="确认新密码:" prop="rePassword">
                <el-input v-model="updatePasswordForm.rePassword"
                          show-password
                          clearable
                          placeholder="请再次输入新密码"
                          style="width: 190px"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button type="primary" @click="updatePassword">确 定</el-button>
            <el-button @click="updatePasswordDialog = false" style="float:right;">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    export default {
        name: "Home",
        data() {
            let checkPassword = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.updatePasswordForm.newPassword) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                user: JSON.parse(window.sessionStorage.getItem('userDate')),
                functions: [
                    {
                        name: "人员管理",
                        id: 1,
                        icon:"el-icon-menu"
                    },{
                        name: "人员列表",
                        id: 2,
                        icon:"el-icon-user"
                    }
                ],
                updatePasswordDialog: false,
                updatePasswordForm: {
                    oldPassword: '',
                    newPassword: '',
                    rePassword: ''
                },
                rules: {
                    oldPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'}
                    ],
                    newPassword: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                        {min: 6, message: '密码最少为6位', trigger: 'blur'},
                        {
                            pattern: '^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$).{6,20}$',
                            message: '密码包含 数字,英文,字符中的两种以上，长度6-20'
                        }
                    ],
                    rePassword: [
                        {required: true, message: '请再次输入密码', trigger: 'blur'},
                        {validator: checkPassword, trigger: 'blur'}
                    ]
                },

            }
        },
        methods: {
            commandHandler(command) {
                if (command === 'logout') {
                    this.$confirm('此操作将注销登录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //注销登录
                        this.getRequest('/logout?token=' + this.user.token);
                        window.sessionStorage.removeItem('userDate');
                        this.$router.push('/');
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消操作'
                        });
                    });

                }
                if (command === 'updatePassword') {
                    this.updatePasswordForm = {
                        oldPassword: '',
                        newPassword: '',
                        rePassword: ''
                    }
                    this.updatePasswordDialog = true
                }
            },
            getView(id) {
                if (id === 1) {
                    this.$router.push('/manager')
                }else if(id===2){
                    this.$router.push('/list')
                }
            },
            updatePassword() {
                this.$refs['updatePasswordForm'].validate((valid) => {
                    if (valid) {
                        let updatePassword = {
                            token: this.user.token,
                            oldPassword: this.updatePasswordForm.oldPassword,
                            newPassword: this.updatePasswordForm.newPassword
                        }
                        this.postRequest('/updatePassword', updatePassword).then(resp => {
                            if (resp) {
                                this.updatePasswordDialog = false
                                this.$message({
                                    message: resp.data.message,
                                    type: 'success'
                                })
                            }
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: '请补全表单'
                        })
                        return false;
                    }
                });
            }
        }
    }
</script>

<style>
    .homeHeader {
        background: #409eff;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 15px;

    }

    .homeHeader .tittle {
        font-size: 30px;
        font-family: 华文行楷;
        color: white;
    }

    .homeHeader .userInfo {
        cursor: pointer;
    }

    .el-dropdown-link img {
        width: 48px;
        height: 48px;
        border-radius: 24px;
        margin-left: 8px;
    }

    .homeWelcome {
        text-align: center;
        font-size: 30px;
        font-family: 华文行楷;
        color: #409eff;
        padding-top: 50px;
    }

    .homeRouterView {
        margin-top: 10px;
    }


</style>
